package com.leather.news.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.news.service.JwService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

@Service
public class JwServiceImpl implements JwService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final static String BASE_URL = "http://jwgl.cqjtu.edu.cn/app.do";


    @Override
    public Result getGrade(String number, String xueqi) {
        String token = getToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(null, headers);
        String getGradeInfo = BASE_URL + "?method=getCjcx&xh=" + number + "&xnxqid=" + xueqi;
        String  result  = "";
        try{
            result = restTemplate.postForObject(getGradeInfo, requestEntity, String.class);
        }catch (Exception e) {
            return Result.error(CodeMsg.NET_OFFLINE);
        }

        JSONArray array = JSON.parseArray(result);
        if (array.get(0) == null) {
            return Result.success(new JSONArray());
        }
        return Result.success(array);
    }

    @Override
    public Result getKS(String number) {
        String token = getToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(null, headers);
        String getKs = BASE_URL + "?method=getKscx&xh=" + number;
        String result = "";
        try {
            result = restTemplate.postForObject(getKs, requestEntity, String.class);
        } catch (Exception e) {
            return Result.error(CodeMsg.NET_OFFLINE);
        }
        JSONArray array = JSON.parseArray(result);
        if (array.get(0) == null) {
            return Result.success(new JSONArray());
        }
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject ;
        for (Object obj : array) {
            jsonObject = (JSONObject) obj;
            jsonArray.add(jsonObject);
        }

        return Result.success(jsonArray);
        /*

            foreach ($data as $k=>$v) {
                $text=$text."考试科目: ".$v->kcmc."\n考试时间: ".$v->ksqssj."\n"."考试地点: ".$v->jsmc."\n\n";
            }

            return $text;
        }
         */


    }

    @Override
    public Result getTodayKebiao(String number) {
        Calendar calendar = Calendar.getInstance();
        int zc = calendar.get(calendar.WEEK_OF_YEAR) - 8;
        String token = getToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(null, headers);
        String getKeBiaoInfo = BASE_URL + "?method=getKbcxAzc&xh=" + number + "&xnxqid=2018-2019-2&zc=" + zc;
        String result = "";
        try {
            result = restTemplate.postForObject(getKeBiaoInfo, requestEntity, String.class);
        } catch (Exception e) {
            return Result.error(CodeMsg.NET_OFFLINE);
        }
        JSONArray array = JSON.parseArray(result);
        if (array.get(0) == null) {
            return Result.success(new JSONArray());
        }
        int i = calendar.get(calendar.DAY_OF_WEEK);
        i--;
        if (i == 0) {
            i = 7;
        }
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        for (Object obj : array) {
            jsonObject = (JSONObject) obj;
            String kcsj = jsonObject.getString("kcsj");
            if (kcsj.startsWith(i + "")) {
                jsonArray.add(obj);
            }
        }
        return Result.success(jsonArray);
    }


    private String getToken() {

        ValueOperations<String, String> operations = redisTemplate.opsForValue();

        String token = operations.get("token");
        if (StringUtils.isBlank(token)) {
            String url = "http://jwgl.cqjtu.edu.cn/app.do?method=authUser&xh=631507020201&pwd=qq1170917384";
            //爬虫获取数据信息
            String responseString = restTemplate.getForObject(url, String.class);
            JSONObject jsonObject = (JSONObject) JSON.parse(responseString);
            token = jsonObject.getString("token");
            operations.set("token", token, 5000, TimeUnit.SECONDS);
        }
        return token;

    }

}
